<?php
class Export {
    function spreadsheet ($server , $database , $user , $password , $query , $filename){
    	
        $connection = mysql_pconnect( $server , $user , $password ) or trigger_error(mysql_error(), E_USER_ERROR);
        
        mysql_select_db ( $database );
        $recordset = mysql_query ( $query , $connection ) or die ( mysql_error ( ) );

        $columns = mysql_num_fields ( $recordset );
        $rows = mysql_num_rows ( $recordset );
        
        $row_recordset = mysql_fetch_array ( $recordset , MYSQL_NUM );
        
        header('Content-type: application/x-msdownload');
        header('Content-Disposition: attachment; filename='.$filename.'.xls');
        header('Pragma: no-cache');
        header('Expires: 0');
        	$spreadsheet = "";
            for ( $i=0 ; $i < $columns ; $i++ ) {
                $spreadsheet .= strtoupper ( mysql_field_name ( $recordset , $i ) ) . "\t";
            }
            for ( $i=0 ; $i < $rows ; $i++ ) {
                $spreadsheet .= "\n";
                    for ( $n=0 ; $n < $columns ; $n++ ) {
                    	if(strtoupper(mysql_field_name ( $recordset , $n )) == "STATUS"){
                    		$row_recordset[$n] = ($row_recordset[$n] == 1)?"Ativo":"Inativo";
                    	}
                        $spreadsheet .= utf8_decode($row_recordset[$n])."\t";
                    }
                $row_recordset = mysql_fetch_array ( $recordset , MYSQL_NUM );                
            }
        print $spreadsheet;
    }

}
?>
